package com.gzhryc.shared_device.oem.code.member.services;

import com.gzhryc.common.StringTools;
import com.gzhryc.common.exceptions.LogicException;
import com.gzhryc.common.logging.Logger;
import com.gzhryc.jdbc.Conditions;
import com.gzhryc.jdbc.MultiDBTools;
import com.gzhryc.shared_device.oem.code.member.dao.MemberCommonDeviceDao;
import com.gzhryc.shared_device.oem.code.member.dao.db.MemberCommonDevice;

import java.sql.SQLException;
import java.util.Date;
import java.util.List;

public class MemberCommonDeviceService {

    static Logger log = Logger.getLogger(MemberCommonDeviceService.class);

    public static MemberCommonDeviceService self() {
        return MultiDBTools.getService(MemberCommonDeviceService.class, true);
    }

    public static MemberCommonDeviceService self(String jdbcKey) {
        return MultiDBTools.getService(jdbcKey, MemberCommonDeviceService.class, true);
    }

    private String jdbcKey;

    public MemberCommonDeviceService(String jdbcKey) {
        this.jdbcKey = jdbcKey;
    }

    public List<MemberCommonDevice> findJoinByMemberId(Long memberId, Integer pageNo, Integer pageLimit){
        if(memberId != null){
            MemberCommonDeviceDao dao = new MemberCommonDeviceDao(jdbcKey,memberId);

            Conditions conditions = new Conditions(MemberCommonDevice.class);
            conditions.and().is("memberId",memberId);

            try {
                return dao.findJoinByCondition(conditions,pageNo,pageLimit);
            } catch (SQLException e) {
                log.error(e.getMessage(),e);
            }
        }
        return null;
    }

    public Long countJoinByMemberId(Long memberId){
        if(memberId != null){
            MemberCommonDeviceDao dao = new MemberCommonDeviceDao(jdbcKey,memberId);

            Conditions conditions = new Conditions(MemberCommonDevice.class);
            conditions.and().is("memberId",memberId);

            try {
                return dao.countJoin(conditions);
            } catch (SQLException e) {
                log.error(e.getMessage(),e);
            }
        }
        return 0L;
    }

    public MemberCommonDevice getByKeys(Long memberId, String deviceSn) {
        if (memberId != null && StringTools.isNotBlank(deviceSn)) {
            MemberCommonDeviceDao dao = new MemberCommonDeviceDao(jdbcKey,memberId);

            Conditions conditions = new Conditions(MemberCommonDevice.class);
            conditions.and().is("deviceSn",deviceSn);
            conditions.and().is("memberId",memberId);

            try {
                return dao.get(conditions);
            } catch (SQLException e) {
                log.error(e.getMessage(), e);
            }
        }
        return null;
    }

    public boolean add(MemberCommonDevice commonDevice)throws LogicException {
        MemberCommonDevice temp = getByKeys(commonDevice.getMemberId(),commonDevice.getDeviceSn());
        if(temp != null){
            return true;
        }
        MemberCommonDeviceDao dao = new MemberCommonDeviceDao(jdbcKey,commonDevice.getMemberId());
        commonDevice.setCreateDate(new Date());
        try {
            return dao.insert(commonDevice) > 0;
        } catch (SQLException e) {
            log.error(e.getMessage(),e);
        }
        return false;
    }

    public boolean deleteByKeys(Long memberId,String deviceSn) throws LogicException{
        if (memberId != null && StringTools.isNotBlank(deviceSn)) {
            MemberCommonDeviceDao dao = new MemberCommonDeviceDao(jdbcKey,memberId);

            Conditions conditions = new Conditions(MemberCommonDevice.class);
            conditions.and().is("deviceSn",deviceSn);
            conditions.and().is("memberId",memberId);

            try {
                return dao.deleteByCondition(conditions) > 0;
            } catch (SQLException e) {
                log.error(e.getMessage(), e);
            }
        }
        return false;
    }

    public boolean deleteByKeys(Long memberId,List<String> deviceSnList){
        if (memberId != null && deviceSnList != null && deviceSnList.size() > 0) {
            MemberCommonDeviceDao dao = new MemberCommonDeviceDao(jdbcKey,memberId);

            Conditions conditions = new Conditions(MemberCommonDevice.class);
            conditions.and().in("deviceSn",deviceSnList);
            conditions.and().is("memberId",memberId);

            try {
                return dao.deleteByCondition(conditions) > 0;
            } catch (SQLException e) {
                log.error(e.getMessage(), e);
            }
        }
        return false;
    }
}
